package by.koleda.photoupdater;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpConnection;

import by.koleda.photoupdater.fragment.LoginDialogFragment;
import by.koleda.photoupdater.helpers.VkOAuthHelper;
import by.koleda.photoupdater.manager.ContentManager;
import by.koleda.photoupdater.manager.HttpManager;
import by.koleda.photoupdater.manager.HttpRequestBuilder;
import by.koleda.photoupdater.manager.HttpRequestBuilder.Type;
import by.koleda.photoupdater.service.HttpService;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
	
	private static final String LOGIN_DIALOG_TAG = "login_dialog_tag";

	private ListView mListView;
	private Button mButton;
	private TextView mTextView;
	private Button mButtonVk;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/*String[] names = getNames();
		mListView = (ListView)findViewById(R.id.listview1);
		mListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, names));
		mButton = (Button)findViewById(R.id.button1);
		mTextView = (TextView)findViewById(R.id.textView1);
		mButtonVk = (Button)findViewById(R.id.connect_btn);*/

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onSyncClick(View view) {
		if (!VkOAuthHelper.isLogged(getApplicationContext()))
		{
			proceedLogin();
		}
		
		startSyncService();
	}
	

	private void startSyncService() {
		HttpRequestBuilder httpRequestBuilder = new HttpRequestBuilder();
		httpRequestBuilder.setType(HttpRequestBuilder.Type.GET);
		httpRequestBuilder.setUrl(ContentManager.FRIENDS_INFO_URL);
		Intent intent = new Intent(this, HttpService.class);
		intent.putExtra(HttpService.HTTP_REQUEST_BUILDER, httpRequestBuilder);
		startService(intent);
	}

	private void proceedLogin() {
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        LoginDialogFragment dialogFragment = new LoginDialogFragment();
    	transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    	transaction.add(dialogFragment, LOGIN_DIALOG_TAG);
    	transaction.commit();
	}
	
	public void onContactsClick(View view) {
		Intent intent = new Intent(MainActivity.this, ContactsActivity.class);
		startActivity(intent);
	}

	public void onConnectToVK(View view) {
		startActivity(new Intent(MainActivity.this, VkLoginActivity.class));
	}
	
	
    public void onButton1Click(View view) {
    	/*Intent intent = new Intent(this, HttpService.class);
    	HttpRequestBuilder httpRequestBuilder = new HttpRequestBuilder();
    	httpRequestBuilder.setType(HttpRequestBuilder.Type.GET);
    	httpRequestBuilder.setUrl(ContentManager.TASKS_URL);
		intent.putExtra(HttpService.HTTP_REQUEST_BUILDER, httpRequestBuilder);
		startService(intent);*/
    	
    	String friendsUrl = ContentManager.FRIENDS_INFO_URL;
    	
    	/*try {
			URL url = new URL(VkOAuthHelper.sign(getApplicationContext(), friendsUrl));
	    	URLConnection connection = url.openConnection();
	    	HttpURLConnection httpConnection = (HttpURLConnection) connection;
	    	
	    	int responseCode = httpConnection.getResponseCode();
	    	if (responseCode == HttpURLConnection.HTTP_OK) {
	    		InputStream in = httpConnection.getInputStream();
	    		BufferedReader text = new BufferedReader(new InputStreamReader(in), 8192);;
	    		StringBuilder sb = new StringBuilder();
				String line = null;
				try {
					while ((line = text.readLine()) != null) {
						sb.append(line)
						.append(System.getProperty("line.separator"));
					}
				} catch (IOException e) {
					throw e;
				}
				String value = sb.toString();
				
	    		mTextView.setText(value.toString());
	    		mTextView.setVisibility(View.VISIBLE);
	    		
	    	}
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	  catch (IOException e) {
    		  mTextView.setText(e.toString());
	    	  mTextView.setVisibility(View.VISIBLE);
			// TODO: handle exception
		}*/
    	
    	/*HttpRequestBuilder request = new HttpRequestBuilder();
    	request.setType(Type.GET);
    	request.setUrl(VkOAuthHelper.sign(getApplicationContext(), friendsUrl);*/
    	try {
			String result = HttpManager.getInstance().loadAsString(VkOAuthHelper.sign(getApplicationContext(), friendsUrl));
    		mTextView.setText(result.toString());
    		mTextView.setVisibility(View.VISIBLE);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
